#include "mainwindow.h"

#include <QDebug>
#include <QGuiApplication>
#include <QScreen>
#include <QRect>
#include <QEvent>
#include <QKeyEvent>
#include <stdio.h>
#include "uisendedittext.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    layoutInit();
    portInit();

    serialPort = new QSerialPort(this);
    connect(btnSend,SIGNAL(clicked()),this,SLOT(sendPushButtonClick()) );
    connect(btnPortOnOff,SIGNAL(clicked()),this,SLOT(openSerialPortPushButtonClicked()) );
    connect(serialPort,SIGNAL(readyRead()),this,SLOT(serialPortReadyRead()) );

    //
    connect(cbPortSendConf_add_lf,SIGNAL(stateChanged(int)),this,SLOT(cbPortSendConf_add_lf_state_change(int)));
}

void MainWindow::portInit()
{
    this ->portNameScan();
    this->portBaudRateItemInit();
    this->portDataBitsItemInit();
    this->portParityItemInit();
    this->portStopBitsItemInit();
    this->portFlowCtrlItemInit();
    serialPortOnOff = 0;

}

void MainWindow::portNameScan()
{
   foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
   {
       cbbPortName -> addItem(info.portName());
   }
}

void MainWindow::portBaudRateItemInit()
{
   QList <QString> list;

   list.append("1200");
   list.append("2400");
   list.append("4800");
   list.append("9600");
   list.append("19200");
   list.append("38400");
   list.append("57600");
   list.append("115200");
   list.append("230400");
   list.append("460800");
   list.append("921600");

   cbbPortBaud->clear();

   for ( int i = 0; i < list.size() ; i++)
       cbbPortBaud->addItem(list[i]);

   cbbPortBaud ->setCurrentIndex(7);
}

void MainWindow::portDataBitsItemInit()
{
   QList <QString> list;
   list.append("5");
   list.append("6");
   list.append("7");
   list.append("8");

   for ( int i = 0 ; i < list.size(); i++)
       cbbPortDataBits ->addItem(list[i]);

   cbbPortDataBits ->setCurrentIndex(3);
}

void MainWindow::portParityItemInit()
{
    QList <QString> list;
    list.append("None");
    list.append("Even");
    list.append("Odd");
    list.append("Space");
    list.append("Mark");

   for ( int i = 0 ; i < list.size(); i++)
      cbbPortCheckBits ->addItem(list[i]);

   cbbPortCheckBits ->setCurrentIndex(0);
}

void MainWindow::portStopBitsItemInit()
{
    QList <QString> list;
    list.append("1");
    list.append("2");

   for ( int i = 0 ; i < list.size(); i++)
       cbbPortStopBits ->addItem(list[i]);

   cbbPortStopBits ->setCurrentIndex(0);
}

void MainWindow::portFlowCtrlItemInit()
{
    QList <QString> list;

    list.append("NoFlowCtrl");
    list.append("HardWardFlowCtrl");
    list.append("SoftWardFlowCtrl");

   for ( int i = 0 ; i < list.size(); i++)
       cbbPortFlowCtrl ->addItem(list[i]);

   cbbPortFlowCtrl->setCurrentIndex(0);

}


QGroupBox* MainWindow:: layoutPortConfInit()
{
    //串口配置
   QVBoxLayout* portConfLayout = new QVBoxLayout();

   QGroupBox* portConfBox = new QGroupBox("串口配置");


   //port name conf
   QGroupBox* portNameBox = new QGroupBox();
   QHBoxLayout* portNameLayout = new QHBoxLayout();
   QLabel* labelPortName = new QLabel("串口号");
   cbbPortName = new QComboBox();

   portNameLayout->addWidget(labelPortName,1);
   portNameLayout->addWidget(cbbPortName,2);
   portNameBox->setLayout(portNameLayout);

   portConfLayout->addWidget(portNameBox);

   //port baud conf
   QGroupBox* portBaudBox = new QGroupBox();
   QHBoxLayout* portBaudLayout = new QHBoxLayout();
   QLabel* labelPortBaud = new QLabel("波特率");
   cbbPortBaud= new QComboBox();

   portBaudLayout->addWidget(labelPortBaud,1);
   portBaudLayout->addWidget(cbbPortBaud,2);
   portBaudBox->setLayout(portBaudLayout);

   portConfLayout->addWidget(portBaudBox);

   //port checkbits
   QGroupBox* portCheckBitsBox = new QGroupBox();
   QHBoxLayout* portCheckBitsLayout = new QHBoxLayout();
   QLabel* labelPortCheckBits = new QLabel("校验位");
   cbbPortCheckBits= new QComboBox();

   portCheckBitsLayout->addWidget(labelPortCheckBits,1);
   portCheckBitsLayout->addWidget(cbbPortCheckBits,2);
   portCheckBitsBox->setLayout(portCheckBitsLayout);

   portConfLayout->addWidget(portCheckBitsBox);

   //port databits
   QGroupBox* portDataBitsBox = new QGroupBox();
   QHBoxLayout* portDataBitsLayout = new QHBoxLayout();
   QLabel* labelPortDataBits = new QLabel("数据位");
   cbbPortDataBits= new QComboBox();

   portDataBitsLayout->addWidget(labelPortDataBits,1);
   portDataBitsLayout->addWidget(cbbPortDataBits,2);
   portDataBitsBox->setLayout(portDataBitsLayout);

   portConfLayout->addWidget(portDataBitsBox);

   //port stopbits
   QGroupBox* portStopBitsBox = new QGroupBox();
   QHBoxLayout* portStopBitsLayout = new QHBoxLayout();
   QLabel* labelPortStopBits = new QLabel("停止位");
   cbbPortStopBits= new QComboBox();

   portStopBitsLayout->addWidget(labelPortStopBits,1);
   portStopBitsLayout->addWidget(cbbPortStopBits,2);
   portStopBitsBox->setLayout(portStopBitsLayout);

   portConfLayout->addWidget(portStopBitsBox);

   //port flowctrol
   QGroupBox* portFlowCtrlBox = new QGroupBox();
   QHBoxLayout* portFlowCtrlLayout = new QHBoxLayout();
   QLabel* labelPortFlowCtrl = new QLabel("流控制");
   cbbPortFlowCtrl= new QComboBox();

   portFlowCtrlLayout->addWidget(labelPortFlowCtrl,1);
   portFlowCtrlLayout->addWidget(cbbPortFlowCtrl,2);
   portFlowCtrlBox->setLayout(portFlowCtrlLayout);

   portConfLayout->addWidget(portFlowCtrlBox);

   //btn on/off
   QGroupBox* portBtnBox = new QGroupBox();
   QHBoxLayout* portBtnLayout = new QHBoxLayout();
   btnPortOnOff = new QPushButton("打开串口");

   portBtnLayout->addWidget(btnPortOnOff);
   portBtnBox ->setLayout(portBtnLayout);

   portConfLayout->addWidget(portBtnBox);

   portConfBox->setLayout(portConfLayout);

   return portConfBox;

}

void MainWindow::layoutLeftInit()
{
   leftLayout = new QVBoxLayout();

   // 接收配置
   QVBoxLayout* recvConfLayout = new QVBoxLayout();

   QGroupBox* recvConfBox = new QGroupBox("接收配置");
   QGroupBox* recvConfBox1 = new QGroupBox("配置1");
   QGroupBox* recvConfBox2 = new QGroupBox("配置2");
   QGroupBox* recvConfBox3 = new QGroupBox("配置3");

   recvConfLayout->addWidget(recvConfBox1);
   recvConfLayout->addWidget(recvConfBox2);
   recvConfLayout->addWidget(recvConfBox3);

   recvConfBox->setLayout(recvConfLayout);


   //
   leftLayout->addWidget(this->layoutPortConfInit(),1);
   leftLayout->addWidget(recvConfBox,1);
   leftLayout->addWidget(this->layoutPortSendConfInit(),5);

}

void MainWindow::cbPortSendConf_add_lf_state_change(int state)
{
    switch (state)
    {
    case Qt::Checked:
        this->portSendConf_add_lf = 1;
        break;
    default:
        this->portSendConf_add_lf = 0;
        break;
    }
}



QGroupBox* MainWindow::layoutPortSendConfInit()
{
   //发送配置
   QVBoxLayout* sendConfLayout = new QVBoxLayout();
   QGroupBox* sendConfBox = new QGroupBox("发送配置");

   //conf1
   QLabel* label1 = new QLabel("行末添加换行");
   QHBoxLayout* sendConfLayout_1 = new QHBoxLayout();
   QGroupBox* sendConfBox1 = new QGroupBox();

   cbPortSendConf_add_lf = new QCheckBox(this);
   cbPortSendConf_add_lf ->setCheckState(Qt::Checked);

   sendConfLayout_1->addWidget(cbPortSendConf_add_lf,1);
   sendConfLayout_1->addWidget(label1,4);
   sendConfBox1->setLayout(sendConfLayout_1);

   sendConfLayout->addWidget(sendConfBox1);
   //
   sendConfBox->setLayout(sendConfLayout);

   return sendConfBox;
}

void MainWindow::layoutRightInit()
{
   rightLayout = new QVBoxLayout();

   //接收
   QGroupBox* recvDataBox = new QGroupBox("接收");
   QVBoxLayout* recvDataLayout = new QVBoxLayout();

   textRecv = new QPlainTextEdit();

   recvDataLayout->addWidget(textRecv);
   recvDataBox->setLayout(recvDataLayout);

   //发送
   QGroupBox* sendDataBox = new QGroupBox("发送");
   QHBoxLayout* sendDataLayout = new QHBoxLayout();

   textEdit = new UiSendEditText();

   textEdit->register_key_enter_handler(this);

   btnSend = new QPushButton("发送");

   sendDataLayout->addWidget(textEdit,9);
   sendDataLayout->addWidget(btnSend,1);

   sendDataBox->setLayout(sendDataLayout);


   rightLayout->addWidget(recvDataBox,5);
   rightLayout->addWidget(sendDataBox,1);
}


void MainWindow::layoutInit()
{
    //获取桌面系统的分辨率
    QList <QScreen*> list_screen = QGuiApplication::screens();

    int screen_w = list_screen.at(0)->geometry().width() ;
    int screen_h = list_screen.at(0)->geometry().height() ;

//    this ->resize(screen_w,screen_h);

    this -> setMinimumSize(screen_w/3,screen_h/3);
//    this -> setMaximumSize(screen_w,screen_h);

    mainWidget = new QWidget();

    //水平的盒子布局
    mainLayout = new QHBoxLayout();

    layoutLeftInit();
    layoutRightInit();

    mainLayout->addLayout(leftLayout,1);
    mainLayout->addLayout(rightLayout,6);

    mainWidget->setLayout(mainLayout);
    this->setCentralWidget(mainWidget);
}

void MainWindow::sendPushButtonClick()
{
    QByteArray data = textEdit ->toPlainText().toUtf8();

    qDebug()<<"data:"<<data;

    if (this->portSendConf_add_lf)
        serialPort->write(data + '\n');
    else
        serialPort->write(data);
}

void MainWindow::openSerialPortPushButtonClicked()
{
    if (serialPortOnOff == 0)
    {
        //set serial name
        serialPort->setPortName(cbbPortName ->currentText());

        //set baud
        serialPort ->setBaudRate(cbbPortBaud ->currentText().toInt());

        //set data bits num

        switch ( cbbPortDataBits ->currentText().toInt())
        {
            case 5:
            serialPort->setDataBits(QSerialPort::Data5);
            break;

            case 6:
            serialPort->setDataBits(QSerialPort::Data6);
            break;

            case 7:
            serialPort->setDataBits(QSerialPort::Data7);
            break;

            case 8:
            serialPort->setDataBits(QSerialPort::Data8);
            break;


        }

        switch(cbbPortCheckBits->currentIndex())
        {
            case 0:
            serialPort->setParity(QSerialPort::NoParity);
            break;

            case 1:
            serialPort->setParity(QSerialPort::EvenParity);
            break;

            case 2:
            serialPort->setParity(QSerialPort::OddParity);
            break;

            case 3:
            serialPort->setParity(QSerialPort::SpaceParity);
            break;

            case 4:
            serialPort->setParity(QSerialPort::MarkParity);
            break;

        }

        //set stop bit
        switch ( cbbPortStopBits ->currentText().toInt())
        {
            case 1:
            serialPort->setStopBits(QSerialPort::OneStop);
            break;

            case 2:
            serialPort->setStopBits(QSerialPort::TwoStop);
            break;

        }

        switch (cbbPortFlowCtrl->currentIndex())
        {
        case 0:
            serialPort->setFlowControl(QSerialPort::NoFlowControl);
            break;

        case 1:
            serialPort->setFlowControl(QSerialPort::HardwareControl);
            break;

        case 2:
            serialPort->setFlowControl(QSerialPort::SoftwareControl);
            break;

        default:
            serialPort->setFlowControl(QSerialPort::NoFlowControl);
            break;

        }

        // set Flow Control

        if ( !serialPort->open(QIODevice::ReadWrite))
        {
            QMessageBox::about(NULL,"错误","串口无法打开！可能串口已经被占用!");
        }
        else
        {
            cbbPortName ->setEnabled(false);
            cbbPortBaud ->setEnabled(false);
            cbbPortCheckBits->setEnabled(false);
            cbbPortDataBits ->setEnabled(false);
            cbbPortStopBits -> setEnabled(false);
            cbbPortFlowCtrl -> setEnabled(false);

            btnSend ->setEnabled(true);
            btnPortOnOff ->setText("关闭串口");
            serialPortOnOff = 1;
        }
    }
    else
    {
        serialPort ->close();

        cbbPortName ->setEnabled(true);
        cbbPortBaud ->setEnabled(true);
        cbbPortCheckBits->setEnabled(true);
        cbbPortDataBits ->setEnabled(true);
        cbbPortStopBits -> setEnabled(true);
        cbbPortFlowCtrl -> setEnabled(true);

        btnPortOnOff->setText("打开串口");
        btnSend ->setEnabled(false);
        serialPortOnOff = 0;
    }
}

void MainWindow::serialPortReadyRead()
{
    QByteArray buf = serialPort->readAll();
    textRecv->insertPlainText(QString(buf));

}


MainWindow::~MainWindow()
{
}

